package com.yb.core.web.util;

import com.yb.core.util.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Optional;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/10 14:13
 * FileName: FileUtil
 * Description:
 */
@Slf4j
public class FileUtil {

    public static String getMd5(File file){
        try {
            return Md5Util.getMd5(new FileInputStream(file));
        } catch (FileNotFoundException e) {
                log.error(String.valueOf(e));
        }
        return "";
    }

    public static String getMimeType(String filename){
        Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(filename);
        if(mediaType.isPresent()){
            return mediaType.get().toString();
        }
        return "";
    }

    public static String getSuffix(String filename){
        String suffix = filename.substring(filename.lastIndexOf(".")+1);
        return suffix;
    }
}
